package kr.co.graviti.adm.bbs.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.graviti.adm.common.dao.AdmCommonDAO;
import kr.co.graviti.common.action.BaseFuncAction;
import kr.co.graviti.common.util.CommonUtil;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AdmBbsReadAction extends BaseFuncAction {
	private static final Logger _log = Logger.getLogger(AdmBbsReadAction.class);

	public ActionForward notice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		boolean _result = new AdmBbsReadAction().boardRead(request, response);
		String returnfoward = "";
		if (_result) {
			returnfoward = CommonUtil.strNullChk(request
					.getParameter("board_type"));
		} else {
			returnfoward = "fail";
		}

		return mapping.findForward(returnfoward);
	}

	/**
	 * 컨텐츠 상세 보기
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public boolean boardRead(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		boolean _result = true;

		HashMap req = null;
		HashMap bbsSort = new HashMap();
		req = CommonUtil.getRequest(request);
		String ibatisNm = "admBbs";

		AdmCommonDAO dao = new AdmCommonDAO();
		/* 게시판 기본정보 */
		int bbsCnt = dao.getIntegerDAO(req, ibatisNm + ".selectBbsSortCount");
		if (bbsCnt > 0) {
			bbsSort = dao.getHashMapDAO(req, ibatisNm + ".selectBbsSort");
		} else {
			String alertMsg = CommonUtil.printAlertBack("잘못된 페이지 정보 입니다..");
			request.setAttribute("alertMsg", alertMsg);
			_result = false;
		}

		req.put("file_type_thumnail", FILE_TYPE_THUMNAIL);
		req.put("file_type_image", FILE_TYPE_IMAGE);
		req.put("file_type_document", FILE_TYPE_DOCUMENT);
		req.put("file_type_movie", FILE_TYPE_MOVIE);

		HashMap readMap = dao.getHashMapDAO(req, ibatisNm + ".selectBbsView");
		String attch_file_idx = CommonUtil.strNullChk(readMap.get(
				"ATTCH_FILE_IDX").toString(), "0");

		// 업로드 파일 정보
		HashMap fileInfoMap = new HashMap();
		fileInfoMap.put("attch_file_idx", attch_file_idx);

		// 썸네일 리스트
		if (Integer.parseInt(readMap.get("THUMCNT").toString()) > 0) {
			fileInfoMap.put("filetype_cd", FILE_TYPE_THUMNAIL);
			List thumFileList = dao.getArrayListDAO(fileInfoMap, ibatisNm
					+ ".selectAttFileInfo");
			request.setAttribute("thumFileList", thumFileList);
		}

		// 이미지 파일 리스트
		if (Integer.parseInt(readMap.get("IMGCNT").toString()) > 0) {
			fileInfoMap.put("filetype_cd", FILE_TYPE_IMAGE);
			List imgFileList = dao.getArrayListDAO(fileInfoMap, ibatisNm
					+ ".selectAttFileInfo");
			request.setAttribute("imgFileList", imgFileList);
		}

		// 첨부 파일 리스트
		if (Integer.parseInt(readMap.get("DOCCNT").toString()) > 0) {
			fileInfoMap.put("filetype_cd", FILE_TYPE_DOCUMENT);
			List docFileList = dao.getArrayListDAO(fileInfoMap, ibatisNm
					+ ".selectAttFileInfo");
			request.setAttribute("docFileList", docFileList);
		}

		// 동영상 파일 리스트
		if (Integer.parseInt(readMap.get("MOVIECNT").toString()) > 0) {
			fileInfoMap.put("filetype_cd", FILE_TYPE_MOVIE);
			List movFileList = dao.getArrayListDAO(fileInfoMap, ibatisNm
					+ ".selectAttFileInfo");
			request.setAttribute("movFileList", movFileList);
		}
		
		// 첨부파일 타입 추가 
		bbsSort.put("thumnail", FILE_TYPE_THUMNAIL);
		bbsSort.put("image", FILE_TYPE_IMAGE);
		bbsSort.put("document", FILE_TYPE_DOCUMENT);
		bbsSort.put("movie", FILE_TYPE_MOVIE);
		
		request.setAttribute("readMap", readMap);
		request.setAttribute("req", req);
		request.setAttribute("bbsSort", bbsSort);
		request.setAttribute("filepath", BBS_FILE_PATH);

		_log.info("==== view ");
		return _result;
	}

}
